package thread;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.Callable;

public class PeopleThread implements Callable {
    private List<Integer> moneys;
    private String name;
    public int totalMoney;
    
    private Random r = new Random();

    public PeopleThread(List<Integer> moneys, String name) {
        this.name = name;
        this.moneys = moneys;
    }

    @Override
    public Integer call() throws Exception {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse("2023-10-08 21:45:40",dateTimeFormatter);
        
        while (LocalDateTime.now().isBefore(start)){
            Thread.sleep(20);
        }
        
        while (true){
            synchronized (moneys){
                if (moneys.size() == 0){
                    break;
                }

                int money = moneys.remove(r.nextInt(moneys.size()));
                System.out.println(name + "抢到" + money);
                totalMoney += money;
                Thread.sleep(10);
            }
        }
        
        return totalMoney;
    }
}
